﻿////////////////////////////////////////////////////////////////////////////////////////////////////
//
//  © 2012 Stanislav S. Yarmonov.
//
//  TODO:LICENSE
//     
////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Web;
using System.Web.Mvc;

namespace Pandora.Web
{
    ////////////////////////////////////////////////////////////////////////////////////////////////
    /// <summary>
    /// Extensions for the System.Web.Mvc.TagBuilder.
    /// </summary>
    ////////////////////////////////////////////////////////////////////////////////////////////////
    public static class TagBuilderExtensions
    {
        ////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Adding attribute for tag builder.
        /// </summary>
        /// <param name="builder">
        /// Builder to add the attribute for.
        /// </param>
        /// <param name="name">
        /// Name of the attribute.
        /// </param>
        /// <param name="value">
        /// Value of the attribute.
        /// </param>
        /// <returns>
        /// Returns tag builder for fluent coding.
        /// </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////
        public static TagBuilder Attribute(this TagBuilder builder, string name, string value)
        {
            builder.MergeAttribute(name, value);
            return builder;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Adding css class attribute for tag builder.
        /// </summary>
        /// <param name="builder">
        /// Builder to add the attribute for.
        /// </param>
        /// <param name="className">
        /// Name of the class to be added.
        /// </param>
        /// <returns>
        /// Returns tag builder for fluent coding.
        /// </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////
        public static TagBuilder Class(this TagBuilder builder, string className)
        {
            builder.MergeAttribute("class", className);
            return builder;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Set HTML inner value for tag.
        /// </summary>
        /// <param name="builder">
        /// Builder to set the HTML for.
        /// </param>
        /// <param name="html">
        /// HTML to be set.
        /// </param>
        /// <returns>
        /// Returns tag builder for fluent coding.
        /// </returns>
        public static TagBuilder Html(this TagBuilder builder, string html)
        {
            builder.InnerHtml = html;
            return builder;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Set HTML inner value for tag.
        /// </summary>
        /// <param name="builder">
        /// Builder to set the HTML for.
        /// </param>
        /// <param name="html">
        /// HTML to be set.
        /// </param>
        /// <returns>
        /// Returns tag builder for fluent coding.
        /// </returns>
        public static TagBuilder Html(this TagBuilder builder, IHtmlString html)
        {
            builder.InnerHtml = html.ToString();
            return builder;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Converts TabBuilder resulting value to IHtmlString implementation.
        /// </summary>
        /// <param name="builder">
        /// Builder to convert.
        /// </param>
        /// <returns>
        /// Returns HTML code.
        /// </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////
        public static IHtmlString ToHtmlString(this TagBuilder builder)
        {
            return MvcHtmlString.Create(builder.ToString());
        }
    }
}
